package com.kkd.kkdapp.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.utils.DialogUtils;

/**
 * 关于
 * 
 * @author mty
 */
public class AboutActivity extends BaseActivity implements OnClickListener {
	private Dialog mDialog;
	private TextView title_tv; // 标题名
	private RelativeLayout with_tickling; // 意见反馈
	private RelativeLayout title_img; // 返回
	private RelativeLayout about_rl; // 官网
	private TextView official; // 获取官网网站
	private AlertDialog dialog;
	private RelativeLayout about_qqu;
	private TextView about_qh;
	private RelativeLayout send_mailbox; // 发送邮箱
	private TextView recommend_tv;
	private Handler mHandler = new Handler();
	private boolean flag = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_with);
		initView();
		setListener();
		setTitle();
	}

	/**
	 * 初始化组件
	 */
	public void initView() {
		title_tv = (TextView) this.findViewById(R.id.title_tv);
		with_tickling = (RelativeLayout) this.findViewById(R.id.with_tickling);
		title_img = (RelativeLayout) findViewById(R.id.title_img);
		about_rl = (RelativeLayout) this.findViewById(R.id.about_rl);
		official = (TextView) this.findViewById(R.id.official);
		about_qqu = (RelativeLayout) this.findViewById(R.id.about_qqu);
		about_qh = (TextView) this.findViewById(R.id.about_qh);
		send_mailbox = (RelativeLayout) this.findViewById(R.id.send_mailbox);
		recommend_tv = (TextView) this.findViewById(R.id.recommend_tv);
		recommend_tv
				.setText("\u3000\u3000"
						+ "快快贷作为快快金融信息服务(上海)有限公司的信贷互联网专业服务平台，秉承诚信、透明、公开、高效的原则，依托互联网企业的网络融资服务平台和电子商务平台，以客户利益为中心，为客户提供全方位、个性化、多样化的理财产品和借款方式，致力于打造中国最具有专业性的创新型互联网金融综合借贷平台。");
		try {
			((TextView) findViewById(R.id.app_version))
					.setText(" "+getPackageManager().getPackageInfo(
							this.getPackageName(), 0).versionName);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 注册监听
	 */
	public void setListener() {
		with_tickling.setOnClickListener(this);
		title_img.setOnClickListener(this);
		about_rl.setOnClickListener(this);
		about_qqu.setOnClickListener(this);
		send_mailbox.setOnClickListener(this);
	}

	/**
	 * 设置标题名
	 */
	public void setTitle() {
		title_tv.setText(R.string.about_title);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.with_tickling:
			showDialog("客服电话：4000-137-000");
			break;
		case R.id.title_img: // 返回
			finish();
			break;
		case R.id.about_rl: // 官网
			intent = new Intent();
			intent.setAction("android.intent.action.VIEW");
			Uri content_url = Uri.parse("http://"+ official.getText().toString());
			intent.setData(content_url);
			startActivity(intent);
			break;
		case R.id.about_qqu:
			if (flag) {
				flag = false;
				String qq = about_qh.getText().toString();
				copy(qq, this);
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						flag = true;
					}
				}, 2000);
			}
			break;
		case R.id.send_mailbox:
			showmobDialog("客服邮箱：kkdai@kkdai.com.cn");
			break;
		}

	}

	public void showDialog(String message) {

		if (mDialog != null) {
			mDialog.cancel();
			mDialog = null;
		}
		mDialog = DialogUtils.deleteBankDialog(this, message,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						switch (v.getId()) {
						case R.id.confirm_deletebank:
							Intent intent = new Intent(Intent.ACTION_CALL, Uri
									.parse("tel:4000137000"));
							startActivity(intent);
							break;
						case R.id.cancel_deletebank:
							mDialog.dismiss();
							break;
						}
					}
				});
		mDialog.show();

	}

	public void showmobDialog(String aboutEmail) {

		if (mDialog != null) {
			mDialog.cancel();
			mDialog = null;
		}
		mDialog = DialogUtils.deleteBankDialog(this, "客服邮箱：kkdai@kkdai.com.cn",
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						switch (v.getId()) {
						case R.id.confirm_deletebank:
							try {
								Uri uri = Uri
										.parse("mailto:kkdai@kkdai.com.cn");
								Intent intent = new Intent(
										Intent.ACTION_SENDTO, uri);
								startActivity(intent);
							} catch (Exception e) {
								mDialog.dismiss();
							}
							break;
						case R.id.cancel_deletebank:
							mDialog.dismiss();
							break;
						}
					}
				});
		mDialog.show();
	}
}
